﻿namespace ConsoleApp3
{
    //模式匹配
    internal class Program
    {
        static void Main(string[] args)
        {
            //类型模式匹配
            object obj = "shang";

            if (obj is string text)
            {
                // 在这里 text 是一个 string 类型的局部变量，可以直接使用
                Console.WriteLine(text.Length);
            }
            Console.WriteLine("*************");

            //Switch 表达式,它支持在 case 分支中使用模式匹配
            object data = 2023;
            switch (data)
            {
                case int number:
                    Console.WriteLine($"It's an integer: {number}");
                    break;
                case string test:
                    Console.WriteLine($"It's a string: {test}");
                    break;
                default:
                    Console.WriteLine("It's something else");
                    break;
            }
            Console.WriteLine("*************");

            //属性模式匹配
            Person person = new Person();
            if (person is { age: 23, name: "shang" })
            {
                Console.WriteLine("The match was successful");
            }
            else
            {
                Console.WriteLine("The match was failed");
            }
            Console.WriteLine("*************");

            //模式变量
            if (person is { age: var age, name: "shang" })
            {
                Console.WriteLine($"shang's age is {age}");
            }
            else
            {
                Console.WriteLine("Not found!");
            }

            //递归模式匹配
            
        





    }
    }

    //属性模式匹配
    class Person
    {
        public string name { get; set; } = "shang";
        public int age { get; set; } = 23;
    }

}